<?php
/**
 * focuscontroller.php
 * 
 * Create on 2015-06-08 15:19:08
 * Create by yuebin
 */
namespace haibao\cms\business\control\wap\common;
use \haibao\cms\business\control\PublishController;

class FocusController extends \haibao\cms\business\control\Base{
	
	const FOCUE_TYPE_EDIT_DATA = 1;
	const FOCUE_TYPE_PC_DATA = 2;
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){
		
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$path = $this->generateHtmlPath(__CLASS__);
		$parameters = array();
		
		if($controlData['manual']){
			
			$focusType = isset($controlData['manual']['focusType'])?$focusType = $controlData['manual']['focusType']:1;
			if($focusType == self::FOCUE_TYPE_EDIT_DATA){
			    $pcData = array();
			    $wapData = array();
			    $controlId = isset($controlData['manual']['controlId'])?$controlData['manual']['controlId']:0;
			    if($controlId){
			        $pcData = $this->getPcControlData($controlId);
			    }
			    $wapData = $this->getControlParameters($controlData);
			    if($wapData){
			        if($pcData){
			            array_push($parameters, $pcData[0]);
			            if(count($pcData)>1){
			                array_push($parameters, $pcData[1]);
			            }
			        }
			        
			        foreach ($wapData as $k=>$v){
			            array_push($parameters, $v);
			        }
			        if(count($pcData)>2){
			            array_splice($pcData,0,2);
			            foreach ($pcData as $key=>$val){
			                array_push($parameters, $val);
			            }
			        }
			    }else{
			        $parameters = $pcData;
			    }
			    if(count($parameters)>6){
			        $parameters = array_splice($parameters,0,6);
			    }
			}elseif(self::FOCUE_TYPE_PC_DATA){
				$controlId = isset($controlData['manual']['controlId'])?$controlData['manual']['controlId']:0;
				if($controlId){
					$parameters = $this->getControlDataById($controlId);
				}
			}
		}
		$this->assign('parameters', $parameters);
		$this->generateHtml($path,$content);
		return $this->getHtml($path);
	}
	
	/**
	 * 获取控件手动数据
	 * @param Int $controlId
	 */
	private function getControlDataById($controlId){
		
		$controls = array();
		$controlData = PublishController::getPageControlDataByControlId($controlId);
		if($controlData){
			foreach($controlData as $key=>$data){
				array_push($controls,json_decode($data->ParameterValue));
			}
		}
		return $controls;
	}
	
	private function getPcControlData($controlId,$focusCount=0){
		$parameters = array();
	    $pcFocusParameters = $this->getControlDataById($controlId);
	    if($pcFocusParameters){
	        foreach($pcFocusParameters as $focusParameters){
	            if((strpos($focusParameters->href, 'article') !== false || strpos($focusParameters->href, 'pics') !== false)){
	                $pcParameters = array();
	                $title = '';
	                if(!empty($focusParameters->title)){
	                    $title = $focusParameters->title;
	                } elseif(!empty($focusParameters->Title)){
	                    $title = $focusParameters->Title;
	                }
	    
	                if(!empty($focusParameters->shorttitle)){
	                    $title = $focusParameters->shorttitle;
	                } elseif(!empty($focusParameters->shortTitle)){
	                    $title = $focusParameters->shortTitle;
	                }
	    
	                $pcParameters['title'] = $title;
	                $pcParameters['href'] = $focusParameters->href;
	                $pcParameters['url'] = $focusParameters->url;
	                $pathArr = pathinfo($pcParameters['href']);
	                $pathInfo = parse_url($pcParameters['href']);
	                $urlHost = explode('.', $pathInfo['host']);
	                if(isset($urlHost['0']) && $urlHost['0']){
	                    $domain = $urlHost['0'];
	                }else{
	                    $domain = 'www';
	                }
	                if(strpos($focusParameters->href, 'article') !== false){
	                    $pcParameters['href'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/'.$domain.'/articlem/'.$pathArr['basename'];
	                } else {
	                    //$pcParameters['href'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_WAP).'/www/pics/'.$pathArr['basename'];
	                    $pcParameters['href'] = preg_replace('/http\:\/\/pic\.haibao\.com.*/isU','http://m.haibao.com/',$focusParameters->href);
	                }
	                $parameters[] = json_decode(json_encode($pcParameters));
	            }
	        }
	    }
	    return $parameters;
	}
}